package com.plm.htmlfiledownload.common;

import java.io.*;
import java.sql.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: Util
 * @Description: Util
 * @Author pengliming
 * @Date 14:25 2019/6/12
 */
public class Util {
    public static boolean isNullOrEmpty(Object obj) {
        if (obj instanceof Object[]) {
            Object[] o = (Object[]) obj;
            for (int i = 0; i < o.length; i++) {
                Object object = o[i];
                if (object instanceof Date) {
                    if (object.equals(new Date(0)))
                        return true;
                } else if ((object == null) || (("").equals(object))) {
                    return true;
                }
            }
        } else {
            if (obj instanceof Date) {
                if (obj.equals(new Date(0))) {
                    return true;
                }
            } else if (obj instanceof Map) {
                if (obj == null || ((Map) obj).isEmpty()) {
                    return true;
                }
            } else if (obj instanceof List) {
                if (obj == null || ((List) obj).isEmpty()) {
                    return true;
                }
            } else if ((obj == null) || (("").equals(obj))) {
                return true;
            }
        }

        return false;
    }

    /*读取文件的字节数组*/
    public static byte[] toByteArray(File file) throws IOException {
        File f = file;
        if (!f.exists()) {
            throw new FileNotFoundException("file not exists");
        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream((int) f.length());
        BufferedInputStream in = null;
        try {
            in = new BufferedInputStream(new FileInputStream(f));
            int buf_size = 1024;
            byte[] buffer = new byte[buf_size];
            int len = 0;
            while ((len = in.read(buffer, 0, buf_size)) != -1) {
                bos.write(buffer, 0, len);
            }
            return bos.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
            throw e;
        } finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            bos.close();
        }
    }
}
